using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entites;
namespace DAL
{
   public class CTDealDAL
    {
        
        
            private SqlConnection cn = null;
        public CTDealDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
       public void Insert (CTDeal ctd )
        {
            SqlCommand cm = new SqlCommand("CTDeal_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTTDeal",ctd.mattdeal);
           cm.Parameters.AddWithValue("@MaDH",ctd.madh);
           
            cm.ExecuteNonQuery();
        }
        public void Update(CTDeal ctd)
        {
            SqlCommand cm = new SqlCommand("CTDeal_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDeal", ctd.madeal);
             cm.Parameters.AddWithValue("@MaTTDeal",ctd.mattdeal);
           cm.Parameters.AddWithValue("@MaDH",ctd.madh);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("CTDeal_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDeal", iD);
            cm.ExecuteNonQuery();
        }

        public tbCTDealCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("Deal_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbCTDealCollection ctdCollection = new tbCTDealCollection();

            while (dr.Read())
            {
                CTDeal th = new CTDeal();
                th.madeal = int.Parse(dr.GetValue(0).ToString());
                th.mattdeal = int.Parse(dr.GetValue(1).ToString());
                th.madh = int.Parse(dr.GetValue(2).ToString());
              
               
                ctdCollection.Add(th);
             }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ctdCollection;
        }
    }
    
    
}
